import { CodeEnum } from '@/constants/http-code'

import { R, P } from './type'

/**
 * 新增请求
 * @param res 响应结果
 * @param success 成功回调
 * @param error 失败回调
 */
export const addReq = (res: any, success: Function, error?: Function) => {
  if (res.code === CodeEnum.INSERT) {
    success()
  } else {
    error ? error() : ''
  }
}

/**
 * 删除请求
 * @param res 响应结果
 * @param success 成功回调
 * @param error 失败回调
 */
export const deleteReq = (res: any, success: Function, error?: Function) => {
  if (res.code === CodeEnum.DELETE) {
    success(res)
  } else {
    error ? error() : ''
  }
}

/**
 * 更新请求
 * @param res 响应结果
 * @param success 成功回调
 * @param error 失败回调
 */
export const updateReq = (res: any, success: Function, error?: Function) => {
  if (res.code === CodeEnum.UPDATE) {
    success()
  } else {
    error ? error() : ''
  }
}

/**
 * 查询请求
 * @param res 响应结果
 * @param success 成功回调
 * @param error 失败回调
 */
type Success = (data: any) => void
export const queryReq = (res: any, success: Success, error?: Function) => {
  if (res.code === CodeEnum.OK) {
    success(res.data)
  } else {
    error ? error() : ''
  }
}

type pageQuerySuccess = (data: { data: Array<any>; total: number }) => void
export const pageQueryReq = (
  res: any,
  success: pageQuerySuccess,
  error?: Function
) => {
  if (res.code === CodeEnum.OK) {
    success({
      data: res.data.list,
      total: res.data.total,
    })
  } else {
    error ? error() : ''
  }
}
